package org.example.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.example.pojo.Article;
import org.springframework.jdbc.core.SqlProvider;

import java.util.List;

@Mapper
public interface ArticleMapper {
    @Insert("insert into article(title,content,cover_img,state,category_id,create_user,create_time,update_time)" +
    "values(#{title}, #{content}, #{coverImg}, #{state}, #{categoryId}, #{createUser}, #{createTime}, #{updateTime})")
    void add(Article article);


    @SelectProvider(type = SqlProvider.class, method = "list")
    List<Article> list(Integer userId, Integer categoryId, String state);

    class SqlProvider {
        public String list(final Integer categoryId, final Integer state, final Integer userId) {
            return new SQL() {{
                SELECT("*");
                FROM("article");
                WHERE("create_user = #{userId}");
                if (categoryId != null) {
                    WHERE("category_id = #{categoryId}");
                }
                if (state != null) {
                    WHERE("state = #{state}");
                }
            }}.toString();
        }
    }


}
